
/**
 * Project Name:cats-core-impl
 * File Name:Menudao.java
 * Package Name:com.catsic.core.menu.dao
 * Date:2014年3月24日下午3:17:00
 * Copyright (c) 2014, oa.catsic.com All Rights Reserved.
 */
 
package com.catsic.core.menu.dao;

import java.util.List;

import javax.persistence.QueryHint;

import org.springframework.data.jpa.repository.Query;
import org.springframework.data.jpa.repository.QueryHints;

import com.catsic.base.BaseDao;
import com.catsic.base.accordion.Accordion;
import com.catsic.core.entity.Menu;

/**
 * ClassName: Menudao
 * Description: 菜单Dao
 * date: 2014年3月24日 下午3:17:00
 * author: yangyd
 */
public interface MenuDao extends BaseDao<Menu,String>{
	
	/**
	 * findByName:通过名称获得菜单列表.
	 * @author yangyd
	 * @param name
	 * @return
	 */
	public List<Menu> findByName(String name);
	
	/**
	 * 通过父节点查询所有子节点,按code升序排列
	 * @param id
	 * @return
	 */
	@Query("select m from Menu m where m.parent.id = ?1 order by m.code asc")
	@QueryHints({ @QueryHint(name = "org.hibernate.cacheable", value ="true") })
	public List<Menu> findByPid(String id);
	
	/**
	 * 获取所有资源
	 * @param id
	 * @return
	 */
	@Query("select m from Menu m order by m.sort asc")
	@QueryHints({ @QueryHint(name = "org.hibernate.cacheable", value ="true") })
	public List<Menu> findAllBySort();
	
	/**
	 * findByUserIdOrderByCodeDesc:用户拥有的菜单
	 * @author yangyd
	 * @param 用户ID
	 * @return
	 */
	@Query("select distinct m from User u join u.roles r join r.menus m where u.id = ?1 order by m.sort asc")
	@QueryHints({ @QueryHint(name = "org.hibernate.cacheable", value ="true") })
	public List<Menu> findByUserIdOrderByCodeAsc(String id);
	
	/**
	 * findByUserIdOrderByCodeDesc:用户拥有的菜单
	 * @author yangyd
	 * @param 用户ID
	 * @return
	 */
	@Query("select new com.catsic.base.accordion.Accordion(m.id,m.name,m.parent.id,m.path) from User u join u.roles r join r.menus m where u.id = ?1 and m.id='100100' order by m.code asc")
	@QueryHints({ @QueryHint(name = "org.hibernate.cacheable", value ="true") })
	public List<Accordion> findByUserIdOrderByCodeDesc1(String id);
	
	/**
	 * 通过父节点查询所有子节点个数
	 * @param id
	 * @return
	 */
	@Query("select count(*) from Menu m where m.parent.id = ?1")
	public Integer findCountByPid(String id);
	
	/**
	 * findByCodeIsNotNullAndParentIdOrderByCodeDesc:查询父节点下所有编码不为空的子节点,按编码倒叙排列
	 * @author yangyudi
	 * @param id
	 * @return
	 */
	@Query("select m from Menu m where m.parent.id = ?1 order by m.code desc")
	@QueryHints({ @QueryHint(name = "org.hibernate.cacheable", value ="true") })
	public List<Menu> findByCodeIsNotNullAndParentIdOrderByCodeDesc(String id);
	
	/**
	 * findByCode:通过CODE获得菜单.
	 * @author yangyd
	 * @param name
	 * @return
	 */
	//@Query("select r from Menu r where r.code = ?1")
	@QueryHints({ @QueryHint(name = "org.hibernate.cacheable", value ="true") })
	public Menu findByCode(String code);
	
	/**
	 * findByLikeCode:通过CODE获得菜单列表.
	 * @author yangyd
	 * @param name
	 * @return
	 */
	@Query("select r from Menu r where r.code like ?1 order by r.code asc")
	@QueryHints({ @QueryHint(name = "org.hibernate.cacheable", value ="true") })
	public List<Menu> findByLikeCode(String code);
	
	@Query("select r from Menu r where r.code like ?1 and r.displayArea.code = ?2 and r.systemType.code = ?2 order by r.code asc")
	@QueryHints({ @QueryHint(name = "org.hibernate.cacheable", value ="true") })
	public List<Menu> findByDrawerCode(String menuCode, String displayArea, String systemType);
	
}
